﻿using FirstTry.DB;
using FirstTry.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FirstTry.CTR
{
    class ReservationCtr
    {
        BatteryCtr bctr = new BatteryCtr();
        DBReservations dbr = new DBReservations();
        DBLeased dbl = new DBLeased();

        public ReservationCtr()
        {
        }

        public void makeReservation(Location l, User u)
        {
            Random ran = new Random();

            List<Battery> locationBattery = bctr.getBatteryByLocation(l.ID.ToString());
            List<Reservation> reservationsByBattery = findReservationsByBatteryList(locationBattery);
            List<Battery> firstBatteryList = removeReservatedBattery(locationBattery, reservationsByBattery);
            List<Leased> leasedList = findLeasedByBatteryList(firstBatteryList);
            List<Battery> final = removeLeasedBattery(firstBatteryList, leasedList);

            int nr = ran.Next(final.Count);

            Battery b = final[0];

            Reservation r = new Reservation(u, b);

            dbr.addReservation(r);
        }

        public void putAsLeased(Reservation r, bool done)
        {
            Leased l = new Leased(r, done);
            dbl.addLeased(l);
            dbr.deleteReservation(r);
        }

        public void updateLeased(Leased l)
        {
            dbl.updateLeased(l);
        }







        private List<Reservation> findReservationsByBatteryList(List<Battery> bl)
        {
            List<Reservation> reservationBattery = new List<Reservation>();

            for (int a = 0; a < bl.Count; a++)
            {
                List<Reservation> rl = dbr.getReservationByBatteryID(bl[a]);

                for (int i = 0; i < rl.Count; i++)
                {
                    reservationBattery.Add(rl[i]);
                }
            }

            return reservationBattery;
        }

        private List<Leased> findLeasedByBatteryList(List<Battery> bl)
        {
            List<Leased> leasedBattery = new List<Leased>();

            for (int a = 0; a < bl.Count; a++)
            {
                List<Leased> rl = dbl.getLeasedByBatteryID(bl[a]);

                for (int i = 0; i < rl.Count; i++)
                {
                    leasedBattery.Add(rl[i]);
                }
            }

            return leasedBattery;
        }

        private List<Battery> removeReservatedBattery(List<Battery> bl, List<Reservation> rl)
        {
            List<Battery> finalList = bl;

            for (int i = 0; i < rl.Count; i++)
            {
                for(int a = 0; a < bl.Count; a++)
                {
                    if (finalList[a].ID == rl[i].b.ID)
                    {
                        finalList.Remove(finalList[a]);
                    }
                }
            }

            return finalList;
        }

        private List<Battery> removeLeasedBattery(List<Battery> bl, List<Leased> ll)
        {
            List<Battery> finalList = bl;

            for (int i = 0; i < ll.Count; i++)
            {
                for (int a = 0; a < finalList.Count; a++)
                {
                    if (finalList[a].ID == ll[i].b.ID)
                    {
                        finalList.Remove(finalList[a]);
                    }
                }
            }

            return finalList;
        }

    }
}
